home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / libs / sphigs / sph_mac.hqx / SRGP port to 5.0 (compressed) / SRGP_SPHIGS Root / MacSPHIGS / sph_falloc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-02-13  |  2.7 KB  |  130 lines

  1. #include "HEADERS.h"
  2. /** FALLOC : falloc.c
  3. **/
  4.  
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <assert.h>
  8. #include "falloc.h"
  9. #include "fallocdefs.h"
  10.  
  11. #include "sph_falloc.proto.h"
  12.  
  13. /** FALLOCnew_chunk
  14. Effects: Allocates a new chunk.
  15. **/
  16.  
  17. FALLOCchunk *
  18. FALLOCnew_chunk(void)
  19. {
  20.   register FALLOCchunk *chunk;
  21.  
  22.   /* These are tiny, so we can just fatal error if they fail */
  23.  
  24.   MALLOC_FATAL(chunk, FALLOCchunk, 1, "new chunk");
  25.   MALLOC_FATAL(chunk->blocks, char *, 1, "first blockptr in new chunk");
  26.   MALLOC_FATAL(chunk->over_blocks, char *, 1, "first blockptr in new chunk");
  27.  
  28.   chunk->magic             = MAGIC;
  29.   chunk->free_bytes      = 0;
  30.   chunk->cur_block       = -1;
  31.   chunk->num_blocks      = 0;
  32.   chunk->num_over_blocks = 0;
  33.  
  34.   return chunk;
  35. }
  36.  
  37. /** FALLOCalloc
  38. Effects: Allocs from the given chunk.
  39. **/
  40.  
  41. char *
  42. FALLOCalloc(FALLOCchunk *chunk, int nbytes, int zero)
  43. {
  44.   register char *ret;
  45.   register int  cb = chunk->cur_block;
  46.   register int  nb = chunk->num_blocks;
  47.   register int  ob = chunk->num_over_blocks;
  48.  
  49.   assert (chunk->magic == MAGIC);
  50.   
  51.   nbytes = (nbytes + ALIGN_SIZE) & ALIGN_MASK;
  52.  
  53.   if (nbytes <= chunk->free_bytes) {
  54.     ret            = chunk->cur_ptr;
  55.     chunk->cur_ptr    += nbytes;
  56.     chunk->free_bytes -= nbytes;
  57.   }
  58.  
  59.   else if (nbytes > FALLOC_BLOCK_SIZE) {
  60.     REALLOC_RET(chunk->over_blocks, char *, ob + 1, (char *) NULL);
  61.     MALLOC_RET(chunk->over_blocks[ob], char, nbytes, (char *) NULL);
  62.     ret = chunk->over_blocks[ob];
  63.     chunk->num_over_blocks++;
  64.   }
  65.  
  66.   else {
  67.     if (++chunk->cur_block >= chunk->num_blocks) {
  68.       REALLOC_RET(chunk->blocks, char *, nb + 1, (char *) NULL);
  69.       MALLOC_RET(chunk->blocks[nb], char, FALLOC_BLOCK_SIZE, (char *) NULL);
  70.       chunk->num_blocks++;
  71.     }
  72.  
  73.     chunk->free_bytes = FALLOC_BLOCK_SIZE - nbytes;
  74.     ret               = chunk->blocks[cb + 1];
  75.     chunk->cur_ptr    = ret + nbytes;
  76.   }
  77.  
  78. #ifdef THINK_C
  79.   if (zero) memset(ret, 0, nbytes);
  80. #else
  81.   if (zero) bzero(ret, nbytes);
  82. #endif
  83.   return ret;
  84. }
  85.  
  86. /** FALLOCfree
  87. Effects: Frees a falloc chunk.
  88. **/
  89.  
  90. void
  91. FALLOCfree(FALLOCchunk *chunk)
  92. {
  93.   register int i;
  94.  
  95.   assert (chunk->magic == MAGIC);
  96.  
  97.   chunk->magic = ~MAGIC;
  98.  
  99.   for (i = 0; i < chunk->num_blocks; i++) FREE(chunk->blocks[i]);
  100.   for (i = 0; i < chunk->num_over_blocks; i++) FREE(chunk->over_blocks[i]);
  101.  
  102.   FREE(chunk->blocks);
  103.   FREE(chunk->over_blocks);
  104.   FREE(chunk);
  105. }
  106.  
  107.  
  108. /** FALLOCclear_chunk
  109. Effects: Clears but does not free a chunk.
  110.          It becomes reusable.
  111. **/
  112.  
  113. void 
  114. FALLOCclear_chunk(FALLOCchunk *chunk)
  115. {
  116.   register int i;
  117.  
  118.   assert (chunk->magic == MAGIC);
  119.  
  120.   chunk->cur_block  = -1;
  121.   chunk->free_bytes = 0;
  122.  
  123.   for (i = 0; i < chunk->num_over_blocks; i++) FREE(chunk->over_blocks[i]);
  124.   chunk->num_over_blocks = 0;
  125.  
  126.   /* No need to free chunk->over_blocks, next realloc will take care of it */
  127. }
  128.  
  129.  
  130.